package com.ctrip.ec.biz.dao.module;
import com.ctrip.ec.biz.entity.module.ModuleRoleEntity;
import com.ctrip.ec.biz.entity.role.RoleUserEntity;
import com.ctrip.ec.dal.DaoBase;
import com.ctrip.ec.biz.entity.module.ModuleEntity;
import com.ctrip.ec.dal.DbAccess;
import com.ctrip.ec.dal.EntityBase;
import com.ctrip.ec.dal.TreeDao;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service
public class ModuleDao extends TreeDao<ModuleEntity> {
    public ModuleDao() {
        super(ModuleEntity.class);
    }
    public ModuleDao(DbAccess db) {
        super(ModuleEntity.class,db);
    }

    public List<ModuleEntity> listByUser(Long userId) throws SQLException {
        String moduleRole = EntityBase.getTableName(ModuleRoleEntity.class);
        String roleUser = EntityBase.getTableName(RoleUserEntity.class);
        String sql = "select * from " + this.getTableName() + " where id in (" +
                     "select module_id from " + moduleRole +" where role_id in (" +
                     "select role_id from " + roleUser + " where user_id =?))";
        List<ModuleEntity> list = this.db.query(ModuleEntity.class,sql,userId);
        return list;
    }
}
